
//倒计时
window.onload = function () {
    var totleSecond = initTimer();
    startTime(totleSecond);   //开始倒计时
}

function initTimer() {
    var totleSecond = Number($("#remainingTime").val())  //获取上图的input的剩余时间
    console.log(totleSecond)
    return totleSecond;
}
function startTime(totleSecond) {
    $("#time").text(formatTime(totleSecond));
    var timer = setInterval(function () {
        totleSecond -= 1;
        $("#remainingTime").val(totleSecond);   //给上图的input更新剩余时间
        if (totleSecond >= 0) {
            $("#time").text(formatTime(totleSecond));
        } else {
            submit();
            alert("考试结束！")
            clearInterval(timer);
            $("#remainingTime").val("");
            window.location.href = '/admin/index'
        }
    }, 1000)
}
function formatTime(totleSecond) {
    var hour = Math.floor(totleSecond / 3600);
    var minute = Math.floor((totleSecond % 3600) / 60);
    var second = Math.floor(totleSecond % 60);

    hour = formatTimeNumber(hour);
    minute = formatTimeNumber(minute)
    second = formatTimeNumber(second);
    return (hour + ":" + minute + ":" + second)
}
function formatTimeNumber(number) {
    if (number < 10) {
        return "0" + number;
    } else {
        return number;
    }
}

//提交答案
function submit() {
    var List = new Array
    const length = document.getElementById("length").value;
    const answer = $('input[name="radio"]:checked').val();
    // const answerId = document.getElementById("answerId").value;
    for (var a = 0; a <= length; a++) {
        console.log(a)
        console.log($("#timu" + a + " .answerId").val())
        // console.log($("#timu" + a + " .answerId")[0].value)
        let tihao = $("#timu" + a + " .answerId").val();
        let daanlength = $("input[name='radio" + a + "']:checked").length
        if (daanlength == 1) {
            // console.log(daan)
            List.push(tihao + '|' + $("input[name='radio" + a + "']:checked").val());
        } else if (daanlength > 1) {
            let daan = ''
            for (let i = 0; i < daanlength; i++) {
                daan += $("input[name='radio" + a + "']:checked")[i].value + ","
            }
            List.push(tihao + '|' + daan)
        }
    }
    console.log("整体："+List);
    console.log(List);

    var params = {
        answerData: List,
        // id: answerId,
        // testNumber:req.session.testNumber,
    };
    $.ajax({
        type: 'POST',
        url: '/admin/exam/updata',
        data: JSON.stringify(params),
        dataType: 'json',
        contentType: "application/json",
        success: function (data) {
            alert("提交成功");
            window.location.href = "/admin/index"
        }
    })
}


$(document).ready(function () {


    // $(function() {
    //     $('.allCard span').click(function() {
    //     // debugger;
    //         console.log("........")
    //         var examId = $(this).closest('.option').attr('value'); // 得到题目ID
    //         console.log("........"+examId)
    //         // var cardLi = $('a[href=#' + examId + ']'); // 根据题目ID找到对应答题卡
    //         // 设置已答题
    //         // if(!cardLi.hasClass('hasBeenAnswer')){
    //         //     cardLi.addClass('hasBeenAnswer');
    //         // }
    //     });
    // });
    //

    $(function () {
        var swiper = new Swiper('.swiper-container', {
            spaceBetween: 30,
            // effect: 'fade',
            pagination: {
                el: '.swiper-pagination',
                clickable: true,
                renderBullet: function (index, className) {
                    return '<span class="' + className + '">' + (index + 1) + '</span>';
                },
            },
            navigation: {
                nextEl: '.swiper-button-next',
                prevEl: '.swiper-button-prev',
            },
        });
    });

});
